#Boa:Dialog:Dialog1

import wx
import wx.grid


CONFIG_BATTLE_FIELD_HOR_SIZE = 10
CONFIG_BATTLE_FIELD_VER_SIZE = 10

def create(parent, app, enemy_name  ):
    return Dialog1(parent, app, enemy_name )

[wxID_DIALOG1, wxID_DIALOG1BUTTONCLOSE, wxID_DIALOG1BUTTONFIRE, 
 wxID_DIALOG1BUTTONUPDATE, wxID_DIALOG1GRID1, wxID_DIALOG1STATICBOX1, 
 wxID_DIALOG1STATICTEXT1, wxID_DIALOG1STATICTEXTENEMY, 
 wxID_DIALOG1STATICTEXTTARGETCOORD, 
] = [wx.NewId() for _init_ctrls in range(9)]

class Dialog1(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DIALOG1, name='', parent=prnt,
              pos=wx.Point(418, 187), size=wx.Size(319, 312),
              style=wx.DEFAULT_DIALOG_STYLE, title=u'Enemy battlefield')
        self.SetClientSize(wx.Size(311, 285))
        self.Bind(wx.EVT_INIT_DIALOG, self.OnDialog1InitDialog)

        self.buttonClose = wx.Button(id=wxID_DIALOG1BUTTONCLOSE, label=u'Close',
              name=u'buttonClose', parent=self, pos=wx.Point(104, 256),
              size=wx.Size(100, 25), style=0)
        self.buttonClose.SetToolTipString(u'Cancel')
        self.buttonClose.Bind(wx.EVT_BUTTON, self.OnButtonCloseButton,
              id=wxID_DIALOG1BUTTONCLOSE)

        self.grid1 = wx.grid.Grid(id=wxID_DIALOG1GRID1, name='grid1',
              parent=self, pos=wx.Point(16, 40), size=wx.Size(280, 168),
              style=0)
        self.grid1.SetColLabelSize(0)
        self.grid1.SetColMinimalAcceptableWidth(15)
        self.grid1.SetDefaultRowSize(15)
        self.grid1.SetDefaultColSize(15)
        self.grid1.SetRowLabelSize(0)
        self.grid1.SetCellHighlightROPenWidth(1)
        self.grid1.EnableEditing(False)
        self.grid1.Enable(True)
        self.grid1.SetSelectionForeground(wx.Colour(0, 0, 0))
        self.grid1.SetScrollLineX(15)
        self.grid1.SetScrollLineY(15)
        self.grid1.SetCellHighlightColour(wx.Colour(255, 255, 255))
        self.grid1.Bind(wx.grid.EVT_GRID_CELL_LEFT_CLICK,
              self.OnGrid1GridCellLeftClick)
        self.grid1.Bind(wx.grid.EVT_GRID_CELL_RIGHT_CLICK,
              self.OnGrid1GridCellRightClick)
        self.grid1.Bind(wx.grid.EVT_GRID_CELL_LEFT_DCLICK,
              self.OnGrid1GridCellLeftDclick)

        self.staticBox1 = wx.StaticBox(id=wxID_DIALOG1STATICBOX1,
              label=u'Battlefield (try left click, left double click, right click)',
              name='staticBox1', parent=self, pos=wx.Point(8, 18),
              size=wx.Size(296, 230), style=0)

        self.buttonUpdate = wx.Button(id=wxID_DIALOG1BUTTONUPDATE,
              label=u'Refresh', name=u'buttonUpdate', parent=self,
              pos=wx.Point(16, 216), size=wx.Size(75, 23), style=0)
        self.buttonUpdate.Bind(wx.EVT_BUTTON, self.OnButtonUpdateButton,
              id=wxID_DIALOG1BUTTONUPDATE)

        self.staticText1 = wx.StaticText(id=wxID_DIALOG1STATICTEXT1,
              label=u'Enemy name:', name='staticText1', parent=self,
              pos=wx.Point(8, 0), size=wx.Size(65, 13), style=0)

        self.buttonFire = wx.Button(id=wxID_DIALOG1BUTTONFIRE, label=u'Fire',
              name=u'buttonFire', parent=self, pos=wx.Point(220, 216),
              size=wx.Size(75, 23), style=0)
        self.buttonFire.Bind(wx.EVT_BUTTON, self.OnButtonFireButton,
              id=wxID_DIALOG1BUTTONFIRE)

        self.staticTextTargetCoord = wx.StaticText(id=wxID_DIALOG1STATICTEXTTARGETCOORD,
              label=u'(x,y)', name=u'staticTextTargetCoord', parent=self,
              pos=wx.Point(106, 221), size=wx.Size(24, 13), style=0)

        self.staticTextEnemy = wx.StaticText(id=wxID_DIALOG1STATICTEXTENEMY,
              label=u'staticTextEnemy', name=u'staticTextEnemy', parent=self,
              pos=wx.Point(79, 0), size=wx.Size(80, 13), style=0)

    def __init__(self, parent, app , enemy_name ):
        self._init_ctrls(parent)
        self.grid1_created = False
        self.app = app 
        self.enemy_name = enemy_name
        self.x = -1
        self.y = -1
        self.SetTitle( self.app.player_name + ' enemy: '+self.enemy_name )
        
    def OnButtonCloseButton(self, event):
        self.Show(False)

    def OnDialog1InitDialog(self, event):
        
        if( self.grid1_created == False ):
            self.grid1.CreateGrid( 10 , 10, 0 )

            self.grid1.DisableDragRowSize()
            self.grid1.DisableDragColSize()
            self.grid1_created = True
            self.staticTextEnemy.SetLabel(self.enemy_name)
            self.OnButtonUpdateButton(None)
            

    def OnButtonUpdateButton(self, event):
        battlefield = self.app.server_proxy.get_enemy_battlefield( self.enemy_name )
        for i in range( 0 , CONFIG_BATTLE_FIELD_VER_SIZE ):
            for j in range( 0 , CONFIG_BATTLE_FIELD_HOR_SIZE ):

                sq = battlefield[ j + i * CONFIG_BATTLE_FIELD_HOR_SIZE ]
                if( sq == 's' ):
                    color = wx.BLACK
                elif( sq == '*' ):
                    color = wx.RED
                elif( sq == '?' ):
                    color = wx.LIGHT_GREY
                elif( sq == '.' ):
                    color = wx.WHITE
                    
                color = self.grid1.SetCellBackgroundColour( i , j , color)
        self.grid1.ForceRefresh()

    def OnGrid1GridCellLeftClick(self, event):
        col = event.GetCol()
        row = event.GetRow()
        self.x = col
        self.y = row
        self.OnButtonUpdateButton(None)
        self.grid1.SetCellBackgroundColour( row , col , wx.BLACK)
        self.grid1.ForceRefresh()
        self.staticTextTargetCoord.SetLabel('('+str(col)+','+str(row)+')')
        
    def OnGrid1GridCellRightClick(self, event):
        self.x = -1
        self.y = -1
        self.staticTextTargetCoord.SetLabel('(No target specified)')

    def OnGrid1GridCellLeftDclick(self, event):
        self.OnGrid1GridCellLeftClick( event )        
        self.OnButtonFireButton(None)

    def OnButtonFireButton(self, event):
        if( self.x != -1 and self.y != -1 ):
            self.app.server_proxy.fire( self.enemy_name , self.x , self.y )
            self.OnButtonUpdateButton(None)
            self.OnGrid1GridCellRightClick(None)
            
            
        

        
